//
//  EMMAnalysisViewController.m
//  EMMApp
//
//  Created by Mac on 16/6/12.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMAnalysisViewController.h"
#import "MenuLitsView.h"
#import "EMMAnalysisCell.h"
#import "ZFChart.h"
#import "GetCustomerAnalysisListApi.h"
#import "GetCustomerVisitAnalysisListApi.h"
#import "EMMAnalysisTwoCell.h"
#import "BSChartCoordinateView.h"
#import "UIColor+BSExt.h"
#import "SNChart.h"

@interface EMMAnalysisViewController ()<UITableViewDelegate,UITableViewDataSource,SNChartDataSource> {
    NSString *num;
    NSString *titleOne;
    NSString *numOne;
    NSString *titleTwo;
    NSString *followString;
    NSString *followStringOne;
    NSInteger touchIndex;
    BOOL _isOne;
    BOOL _istwo;
    NSString *timer;
    NSInteger cHigth;
    NSString *titleThree;
    NSInteger h;
    NSNumber *max;
    int number;
}

@property (nonatomic, strong) UITableView *tableView;

@property (strong, nonatomic) UIView *headBackView;

@property (strong, nonatomic) UIImageView *headImageview;
@property (strong, nonatomic)UIButton *djBtn;
@property (strong, nonatomic)UIButton *btn;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) MenuLitsView *menuOne;
@property (nonatomic, strong) MenuLitsView *menuTwo;

@property (strong, nonatomic) UIView *timerView;
@property (strong, nonatomic) UIButton *timerBtn;

@property (strong, nonatomic) ZFPieChart * pieChart;
@property (strong, nonatomic) ZFLineChart * lineChart;

@property (nonatomic, strong) UILabel *jlLabel;

@property (nonatomic, strong) NSMutableArray *arrNum1;
@property (nonatomic, strong) NSMutableArray *arrNum2;

@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) NSMutableArray *numArr;

@property (nonatomic, weak) BSChartCoordinateView *baseView;
//@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, strong) UIView *zxView;

@property (nonatomic, strong) SNChart *chart;

@end

@implementation EMMAnalysisViewController

//- (NSDateFormatter *)dateFormatter {
//    
//    if (_dateFormatter == nil) {
//        _dateFormatter = [[NSDateFormatter alloc] init];
//        _dateFormatter.dateFormat = @"MM-dd HH:mm";
//    }
//    return _dateFormatter;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    num = @"0";
    numOne = @"0";
    followString = @"1";
    followStringOne = @"1";
    [self creatNavigationController];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    cHigth = 400;
    [self creatDropDownView];
    [self creatTimerShowUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 导航

- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"客户分析";
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
    [backBtn addTarget:self
                action:@selector(backToPreviousPage:)
      forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    if (EMM_WIDTH <= 320) {
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }else if (EMM_WIDTH > 375){
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    }else{
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)backToPreviousPage:(UIButton *)btn {
    [self removeSubViewOne];
    [self removeSubViewTWo];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _arrNum1 = [[NSMutableArray alloc] init];
    _arrNum2 = [[NSMutableArray alloc] init];
    _nameArr = [[NSMutableArray alloc] init];
    _numArr = [[NSMutableArray alloc] init];
    [self showHudInWindow:@"正在加载请稍等..."];

    [self PostGetCustomerAnalysisListApiTimeType:@"1" categoryType:@"1" type:@"0"];
    [self PostGetCustomerVisitAnalysisListApiTimeType:@"1"];
}

- (void)PostGetCustomerAnalysisListApiTimeType:(NSString *)timeType categoryType:(NSString *)categoryType type:(NSString *)type {
    GetCustomerAnalysisListApi *api = [[GetCustomerAnalysisListApi alloc] initWithTimeType:timeType categoryType:categoryType];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetCustomerAnalysisListApi *result = (GetCustomerAnalysisListApi *)request;
        
        if (result.isSuccess) {
            GetCustomerAnalysisList *get = result.resultData;
            NSMutableArray *arr1 = [[NSMutableArray alloc] init];
            NSMutableArray *arr2 = [[NSMutableArray alloc] init];
            for (CusAnalysisList *list in get.cusAnalysisList) {
                [_arrNum1 addObject:list];
                    [arr2 addObject:list.analysisValue];
                    [arr1 addObject:list.analysisValuePercentage];
            }
            NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:
                                    ZFColor(255, 231, 110, 1),ZFColor(255, 197, 153, 1),
                                    ZFColor(255, 180, 103, 1),ZFColor(255, 144, 72, 1),
                                    ZFColor(245, 74, 43, 1), ZFColor(251, 115, 136, 1),
                                    ZFColor(245, 60, 127, 1),ZFColor(243, 53, 94, 1),
                                    ZFColor(204, 31, 26, 1),ZFColor(117, 12, 12, 1),
                                    ZFColor(197, 244, 255, 1),ZFColor(103, 194, 213, 1),
                                    ZFColor(81, 153, 170, 1),ZFColor(4, 151, 150, 1),
                                    ZFColor(8, 150, 104, 1),ZFColor(140, 211, 231, 1),
                                    ZFColor(33, 185, 211, 1),ZFColor(0, 110, 168, 1),
                                    ZFColor(0, 90, 166, 1),ZFColor(91, 84, 119, 1),nil];
            cHigth = 300 +arr1.count * 25;
            h = arr1.count;
            [self showPieChartvalueArray:arr1 nameArray:arr2 colorArray:arr3 cHigth:cHigth];
            if (get.cusAnalysisList.count > 0) {
                _pieChart.hidden =NO;
                _jlLabel.hidden = YES;
            }else {
                _jlLabel.hidden = NO;
                _pieChart.hidden =YES;
            }
        } else {
            cHigth = 300 + h * 25;
            _jlLabel.hidden = NO;
            _pieChart.hidden =YES;
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
//        [_tableView reloadData];
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

- (void)PostGetCustomerVisitAnalysisListApiTimeType:(NSString *)timeType {
    [_arrNum2 removeAllObjects];
    [_nameArr removeAllObjects];
    [_numArr removeAllObjects];
    GetCustomerVisitAnalysisListApi *api = [[GetCustomerVisitAnalysisListApi alloc] initWithVisitTimeType:timeType];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetCustomerVisitAnalysisListApi *result = (GetCustomerVisitAnalysisListApi *)request;
        if (result.isSuccess) {
            GetCustomerVisitAnalysisList *get = result.resultData;
            NSMutableArray *arr1 = [[NSMutableArray alloc] init];
            NSMutableArray *arr2 = [[NSMutableArray alloc] init];
            for (CusVisitAnalysisList *list in get.cusVisitAnalysisList) {
                 [_arrNum2 addObject:list];
                [arr1 addObject:list.visitAnalysisValueNum];
                [arr2 addObject:list.visitAnalysisValue];
                [_nameArr addObject:list.visitAnalysisValueNum];
                [_numArr addObject:list.visitAnalysisValue];
            }
            if (arr1.count > 0) {
                max = [arr1 valueForKeyPath:@"@max.floatValue"];
                number = [max intValue];
                if (number > 10) {
                    [self showLineChartxLineValueArray:arr1 xLineTitleArray:arr2 yLineMaxValue:number yLineSectionCount:10];
                }else {
                    [self showLineChartxLineValueArray:arr1 xLineTitleArray:arr2 yLineMaxValue:10 yLineSectionCount:10];
                }
            }
            
        } else {
            [self showToast:result.resultData.errorMsg];
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:1];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}


#pragma mark - scrollViewDidScroll --> 滑动响应事件
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_mainScrollView.scrollEnabled) {
        [self headLineViewScrollWithIndex:scrollView.contentOffset.x/EMM_WIDTH];
    }
}

#pragma mark - UITableView代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 450;
    }
    return cHigth+45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EMMAnalysisCellId";
    EMMAnalysisCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EMMAnalysisCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.section == 0) {
        [cell addSubview:self.headBackView];
        if (_arrNum1.count > 0) {
            [cell addSubview:_pieChart];            
        }else {
            UILabel *jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300/2, EMM_WIDTH, 20)];
            jlLabel.text = @"当前没有记录";
            jlLabel.textAlignment = NSTextAlignmentCenter;
            jlLabel.font = [UIFont systemFontOfSize:15];
            [cell addSubview:jlLabel];
        }
        
        
    }else if (indexPath.section == 1) {
        
        static NSString *CellIdentifier = @"EMMAnalysisTwoCellId";
        EMMAnalysisTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"EMMAnalysisTwoCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell addSubview:self.timerView];
       
//        _chart.backgroundColor = [UIColor redColor];
//        [cell addSubview:_chart];
       
        
        
        if (_arrNum2.count > 0) {
//            _chart = [[SNChart alloc] initWithFrame:CGRectMake(0, 50, EMM_WIDTH, 400) withDataSource:self andChatStyle:SNChartStyleLine];
//            [_chart showInView:cell];
            [cell initWithXNumArr:_nameArr oneNumArr:_numArr indexPath:indexPath];
        
        }else {
            UILabel *jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300/2, EMM_WIDTH, 20)];
            jlLabel.text = @"当前没有记录";
            jlLabel.textAlignment = NSTextAlignmentCenter;
            jlLabel.font = [UIFont systemFontOfSize:15];
            [cell addSubview:jlLabel];
        }
        
        return cell;
        
    }
    return cell;
}



#pragma mark - 跳出层初始化
- (void)creatDropDownView {//
    self.headBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 45)];
    self.headBackView.backgroundColor = [UIColor colorFromHexCode:@"##fff5e9"];
    
    UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 44.5, EMM_WIDTH, 0.5)];
    l1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [self.headBackView addSubview:l1];
    
    _djBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _djBtn.frame = CGRectMake(0, 5, EMM_WIDTH/2, 32);
    _djBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_djBtn setTitle:@"意向等级" forState:UIControlStateNormal];
    [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    [_djBtn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];    //设置button背景显示图片
    _djBtn.tag = 2001;
   
    [_djBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
//    [_djBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _djBtn.titleLabel.bounds.size.width+EMM_WIDTH/2-50, 0, _djBtn.titleLabel.bounds.size.width)];
    NSLog(@"%f",_djBtn.titleLabel.bounds.size.width);
    [_djBtn addTarget:self action:@selector(headButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBackView addSubview:_djBtn];
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(EMM_WIDTH/2, 5, EMM_WIDTH/2, 32);
//    _btn.backgroundColor = [UIColor orangeColor];
    _btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_btn setTitle:@"今日" forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    _btn.tag = 2002;
    [_btn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];    //设置button背景显示图片
//    [_btn setImageEdgeInsets:UIEdgeInsetsMake(0, _btn.titleLabel.bounds.size.width+EMM_WIDTH/2-60, 0, _btn.titleLabel.bounds.size.width)];
    [_btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [_btn addTarget:self action:@selector(headButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.headBackView addSubview:_btn];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH/2, 7.5, 1, 30)];
    label.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [self.headBackView addSubview:label];
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
    CGSize textSize = [@"意向等级" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    
    _headImageview = [[UIImageView alloc] initWithFrame:CGRectMake((EMM_WIDTH/2-textSize.width)/2-10, 43, textSize.width, 2)];
    _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#5A5A5A"];
    [self.headBackView addSubview:self.headImageview];
    _isOne = YES;
    _istwo = YES;
    
    _jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (cHigth-20)/2, EMM_WIDTH, 20)];
    _jlLabel.text = @"当前没有记录";
    _jlLabel.textAlignment = NSTextAlignmentCenter;
    _jlLabel.font = [UIFont systemFontOfSize:15];
    _jlLabel.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
    _jlLabel.hidden = YES;
}

#pragma mark - 客户分析

- (NSArray *)chatConfigYValue:(SNChart *)chart {
    //    return @[@"0.1",@"0.5",@"0.70",@"0.30",@"0.50",@"0.014",@"0",@"0.14"];
//    return @[@"100",@"50",@"70",@"30",@"50",@"14",@"5",@"14",@"5",@"14"];
    return _nameArr;
}

- (NSArray *)chatConfigXValue:(SNChart *)chart {
//    return @[@"12-1-2016",@"12-2-2016",@"12-3-2016",@"12-4-2016",@"12-5-2016",@"12-6-2016",@"12-7-2016",@"12-8-2016",@"12-9-2016",@"12-10-2016"];
    return _numArr;
}


- (void)showPieChartvalueArray:(NSMutableArray *)valueArray nameArray:(NSMutableArray *)nameArray colorArray:(NSMutableArray *)colorArray cHigth:(NSInteger)gaoduH{
    _pieChart = [[ZFPieChart alloc] initWithFrame:CGRectMake(0, 40, EMM_WIDTH, gaoduH) isInitWith:1];
    _pieChart.valueArray = valueArray;
    _pieChart.nameArray = nameArray;
    _pieChart.colorArray = colorArray;
    _pieChart.percentType = kPercentTypeInteger;
    _pieChart.scrollEnabled = NO;
    [_pieChart strokePath:0];
}

- (void)showLineChartxLineValueArray:(NSMutableArray *)xLineValueArray xLineTitleArray:(NSMutableArray *)xLineTitleArray yLineMaxValue:(int)yLineMaxValue yLineSectionCount:(int)yLineSectionCount{
//    _lineChart = [[ZFLineChart alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 250)];
//    _lineChart.xLineValueArray = xLineValueArray;
//    _lineChart.xLineTitleArray = xLineTitleArray;
//    _lineChart.yLineMaxValue = yLineMaxValue;
//    _lineChart.yLineSectionCount = yLineSectionCount;
//    [_lineChart strokePath:@"#4cb84b"];
    
    if (xLineValueArray.count > 0) {
        NSMutableArray *arrY = [[NSMutableArray alloc] init];;
        for (int i = 0; i < xLineValueArray.count; i++) {
            NSString *sr = [xLineValueArray objectAtIndex:i];
            [arrY addObject:sr];
        }
        NSArray *arr = [NSArray arrayWithObjects:@"0", nil];
        for (int i = 0; i < arr.count; i++) {
            NSString *str = [arr objectAtIndex:i];
            [arrY addObject:str];
        }
        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:nil ascending:YES];
        
        NSArray *sortArray = [NSArray arrayWithObjects:descriptor,nil];
        
        NSArray *sortedArray = [arrY sortedArrayUsingDescriptors:sortArray];
        NSLog(@"---- >>>> %@",sortedArray);
        BSChartCoordinateView *bschartBase = [[BSChartCoordinateView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT)];
        self.baseView = bschartBase;
        [_zxView addSubview:bschartBase];
        [bschartBase configChartCoordinateXAxisWith:[NSMutableArray arrayWithArray:xLineTitleArray] andYAxisWith:sortedArray andXScale:0.4 andYScale:BSPxToPt(3.0) andDataType:BSChartBMPType andXAxisColor:[UIColor colorWithHexString_Ext:@"#959595"] andYAxisColor:[UIColor colorWithHexString_Ext:@"#959595"]];
        NSMutableArray *listArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < xLineValueArray.count; i++) {
            NSString *strNum = [xLineValueArray objectAtIndex:i];
            [listArr addObject:KSStringFromBMPResult(KSChartBMPResultMake([strNum floatValue]*10, 0, 0))];
        }
        NSMutableArray *itemsArr = [NSMutableArray array];
        for (int i = 0; i < listArr.count; i++) {
            BSChartPointItem *pointItem = [[BSChartPointItem alloc] initBMPPointWithXValue:nil andYValue:KSChartBMPFromString(listArr[i]) andItemColor:nil];
            [itemsArr addObject:pointItem];
        }
        
        [self.baseView updateChartDataWithDataSource:itemsArr andDataType:BSChartBMPType];
//        [_tableView reloadData];
    }
   
    
    
    
}


- (void)setScrollView:(UIScrollView *)scrollView width:(CGFloat)width {
    
    scrollView.showsHorizontalScrollIndicator = NO;
    
    scrollView.showsVerticalScrollIndicator = NO;
    
    scrollView.pagingEnabled = YES;
    
    scrollView.bounces = NO;
    
    scrollView.contentSize = CGSizeMake(width,scrollView.bounds.size.height);
    
}


#pragma mark - 顶部按键响应事件
- (void)headButtonClick:(UIButton *)button {
    [self headLineViewScrollWithIndex:button.tag-2001];
    [UIView animateWithDuration:0.5 animations:^{
        _mainScrollView.contentOffset = CGPointMake((button.tag-2001)*ScreenSize.width, 0);
    }];
    
    if (button.tag == 2001) {
        
        if (_isOne == YES ) {
            [self menulistShowUIOne];
            [_djBtn setImage:[UIImage imageNamed:@"sj_up"] forState:UIControlStateNormal];
            _isOne = NO;
            [_btn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];    //设置button背景显示图片

            [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#e97300"] forState:UIControlStateNormal];
            [_btn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
            //设置button背景显示图片
            _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#e97300"];
        }else {
            [self removeSubViewOne];
            [_djBtn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];
            _isOne = YES;
            [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
            _headImageview.backgroundColor = [UIColor grayColor];
        }
        _istwo = YES;
        [self removeSubViewTWo];
        
    }else if (button.tag == 2002) {
//        
//        [self.djBtn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];
//        [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
//        [_btn setTitleColor:[UIColor colorFromHexCode:@"#ff7200"] forState:UIControlStateNormal];
        if (_istwo == YES ) {
            [self menulistShowUITwo];
            [_btn setImage:[UIImage imageNamed:@"sj_up"] forState:UIControlStateNormal];

            _istwo = NO;
            [_djBtn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];    //设置button背景显示图片
            
            [_btn setTitleColor:[UIColor colorFromHexCode:@"#e97300"] forState:UIControlStateNormal];
            [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
            _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#e97300"];
            
        }else {
            [self removeSubViewTWo];
            [_btn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];

            _istwo = YES;
            [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            _headImageview.backgroundColor = [UIColor grayColor];
        }
        _isOne = YES;
        [self removeSubViewOne];
    }
    [_btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [_djBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
}

#pragma mark - 第一个
- (void)menulistShowUIOne {
    //1、意向等级（默认）；2、意向户型；3、需求面积；4、预算总价；5、客户年龄；6、客户状态
    NSArray *arr = [NSArray arrayWithObjects:@"意向等级（默认）",@"关注户型",@"需求面积",@"预算总价",@"客户年龄",@"客户状态", nil];
    _menuOne = [[MenuLitsView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight+45, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-45) items:arr indet:num isShowUI:NO guodu:arr.count*50];
    _menuOne.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:_menuOne];
    __weak MenuLitsView *weakSelf = _menuOne;
    [weakSelf setMenuLitsBlock:^(NSString *title, NSString *indet) {
        [self.djBtn setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];

        if ([NSString isStringEmpty2:indet] == NO) {
            num = indet;
            titleOne = title;
            followString = [NSString stringWithFormat:@"%ld",[indet integerValue]+1];
            cHigth = 400;
            if ([indet isEqualToString:@"0"]) {
                [_djBtn setTitle:@"意向等级" forState:UIControlStateNormal];
            }else {
                [_djBtn setTitle:title forState:UIControlStateNormal];
            }
//            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
//            CGSize textSize = [_djBtn.titleLabel.text boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
//            self.headImageview.frame = CGRectMake((EMM_WIDTH/2-textSize.width)/2+5, 43, textSize.width, 2);
            [self showHudInWindow:@"正在加载请稍等..."];

            [self PostGetCustomerAnalysisListApiTimeType:followStringOne categoryType:followString type:indet];
         
            [_tableView reloadData];
        }else {
            if ([NSString isStringEmpty2:titleOne] == NO) {
                [_djBtn setTitle:titleOne forState:UIControlStateNormal];
            }else {
                [_djBtn setTitle:@"意向等级" forState:UIControlStateNormal];
            }

        }
        [_djBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#5A5A5A"];
        [_djBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
        _isOne = YES;
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - 第一个图时间
- (void)menulistShowUITwo {
 
    NSArray *arr = [NSArray arrayWithObjects:
                    @"今日",@"昨天",@"本周",
                    @"上周",@"本月",@"上月",@"近三月",@"今年", nil];
    _menuTwo = [[MenuLitsView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight+45, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-45) items:arr indet:numOne isShowUI:NO guodu:6*50];
    _menuTwo.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:_menuTwo];
    __weak MenuLitsView *weakSelf = _menuTwo;
    [weakSelf setMenuLitsBlock:^(NSString *title, NSString *indet) {
        [self.btn  setImage:[UIImage imageNamed:@"sj_down"] forState:UIControlStateNormal];
        if ([NSString isStringEmpty2:indet] == NO) {
            numOne = indet;
            titleTwo = title;
            followStringOne = [NSString stringWithFormat:@"%ld",[indet integerValue]+1];
            [self showHudInWindow:@"正在加载请稍等..."];
            [self PostGetCustomerAnalysisListApiTimeType:followStringOne categoryType:followString type:num];
            [_btn setTitle:title forState:UIControlStateNormal];
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [_btn.titleLabel.text boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            self.headImageview.frame = CGRectMake((EMM_WIDTH/2-textSize.width)/2+EMM_WIDTH/2-10, 43, textSize.width, 2);
            
            [_tableView reloadData];
        }else {
            if ([NSString isStringEmpty2:titleOne] == NO) {
                [_btn setTitle:titleTwo forState:UIControlStateNormal];
            }else {
                [_btn setTitle:@"今日" forState:UIControlStateNormal];
            }

        }
        [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
        _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#5A5A5A"];
        _istwo = YES;
        
        [weakSelf removeFromSuperview];
    }];
}

- (void)removeSubViewOne {
    [_menuOne removeFromSuperview];
    _menuOne = nil;
}
- (void)removeSubViewTWo {
    [_menuTwo removeFromSuperview];
    _menuTwo = nil;
}
#pragma mark - 顶部线移动,
- (void)headLineViewScrollWithIndex:(NSInteger)index {
    [UIView animateWithDuration:0.15 animations:^{
        if (index == 0) {
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [_djBtn.titleLabel.text boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            self.headImageview.frame = CGRectMake((EMM_WIDTH/2-textSize.width)/2-10, 43, textSize.width, 2);
        }else if (index == 1) {
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [_btn.titleLabel.text boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            self.headImageview.frame = CGRectMake((EMM_WIDTH/2-textSize.width)/2+EMM_WIDTH/2-10, 43, textSize.width, 2);
        }
    }];
}

- (void)creatTimerShowUI {
    self.timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 40)];
    self.timerView.backgroundColor = [UIColor colorFromHexCode:@"#fff5e9"];

    _timerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _timerBtn.frame = CGRectMake(0, 0, EMM_WIDTH, 40);
    [_timerBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    [_timerBtn setTitle:@"近7天" forState:UIControlStateNormal];
    [_timerBtn addTarget:self action:@selector(timerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _timerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_timerBtn setTitleColor:[UIColor colorFromHexCode:@"#1A1A1A"] forState:UIControlStateNormal];
    [self.timerView addSubview:_timerBtn];
    
    _zxView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, EMM_WIDTH, 400)];
    _zxView.backgroundColor = [UIColor redColor];
    
    
}

#pragma mark - 第二个图
- (void)timerBtnClick:(UIButton *)btn {
    
    NSArray *arr = [NSArray arrayWithObjects:
                    @"近7天（默认）",@"近15天",@"近一月",
                    @"近三月",@"近半年",nil];
    ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"选项" titleNmae:@"选择时间" numLits:nil items:arr];
    menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:menuView];
    __block ShowMenuView *weakMenuView = menuView;//
    __block EMMAnalysisViewController *blockSelf = self;
    [menuView setTouchBlock:^(NSString *indet,NSString *numberString) {
        NSLog(@"%@",indet);
        if ([NSString isStringEmpty2:indet] == NO) {
            titleThree = indet;
            if ([numberString isEqualToString:@"1"]) {
                [_timerBtn setTitle:@"近7天" forState:UIControlStateNormal];

            }else {
                [_timerBtn setTitle:indet forState:UIControlStateNormal];
            }
            timer = numberString;
            [self showHudInWindow:@"正在加载请稍等..."];
            [blockSelf PostGetCustomerVisitAnalysisListApiTimeType:timer];
        }else {
            if ([NSString isStringEmpty2:titleThree] == NO) {
                [_timerBtn setTitle:titleTwo forState:UIControlStateNormal];
            }else {
                [_timerBtn setTitle:@"近7天" forState:UIControlStateNormal];
            }
            
        }
        [weakMenuView removeFromSuperview];
    }];
}


@end
